// pages/bind-phone/bind-phone.js

const createTimer = require('../../utils/timer.js').createTimer
const requestPutRenterPhone = require('../../utils/request.js').requestPutRenterPhone
const toBindBookPage = require('../../utils/wx-navigate.js').toBindBookPage
const requestToPostBindPhoneSmsCode = require('../../utils/request.js').requestToPostBindPhoneSmsCode


//获取应用实例
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    phone:"",
    code:"",
    verify_code_text: "获取验证码",
    verify_code_lock:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },

  bindtap:function(){
    
    if(this.data.phone){
      if (!this.data.verify_code_lock) {
        console.log('----sbsbsbs')
        requestToPostBindPhoneSmsCode(this.data.phone, res => {
          console.log('sbsbsbs')
        })
        let time = 60000
        createTimer(time, timeStart => {
          this.setData({
            verify_code_text: `重新发送(${(time - timeStart) / 1000}s)`,
            verify_code_lock: true
          })
        }, () => {
          this.setData({
            verify_code_text: `重新发送`,
            verify_code_lock: false
          })
        })
      }
    }else{
      console.log('请输入手机号')
    }
    
  },


  bindbtntap(){
    requestPutRenterPhone(this.data.phone, this.data.code,res=>{
      app.updateIndexPageData()
      toBindBookPage()
    })
  },

  bindphoneinput:function(e) {
    let phone = e.detail.value
    this.setData({
      phone: phone
    })
  },

  bindcodeinput: function (e) {
    let code = e.detail.value
    this.setData({
      code: code
    })
  },
})